package com.wsj.service.batchmanagenews.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wsj.dao.IHomeSiteNewsClassDao;
import com.wsj.dao.IHomeSiteNewsDao;
import com.wsj.dao.IUserBaseDao;
import com.wsj.service.batchmanagenews.IBatchUploadNews;
import com.wsj.web.subclass.object.BatchUploadHomeSiteNewsBean;
import com.wsj.web.subclass.object.HeadBean;
import com.wsj.web.vo.homeSite.HomeSiteNews;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;
import com.wsj.web.vo.homeSite.UserBase;

public class BatchUploadNewsImpl implements IBatchUploadNews{

	Log logger = LogFactory.getLog(this.getClass());
	
	private IHomeSiteNewsDao homeSiteNewsDao;
	
	private IHomeSiteNewsClassDao  homeSiteNewsClassDao;
	
	private IUserBaseDao userBaseDao;
	
	
	
	public IUserBaseDao getUserBaseDao() {
		return userBaseDao;
	}





	public void setUserBaseDao(IUserBaseDao userBaseDao) {
		this.userBaseDao = userBaseDao;
	}





	public IHomeSiteNewsDao getHomeSiteNewsDao() {
		return homeSiteNewsDao;
	}





	public void setHomeSiteNewsDao(IHomeSiteNewsDao homeSiteNewsDao) {
		this.homeSiteNewsDao = homeSiteNewsDao;
	}





	public IHomeSiteNewsClassDao getHomeSiteNewsClassDao() {
		return homeSiteNewsClassDao;
	}





	public void setHomeSiteNewsClassDao(IHomeSiteNewsClassDao homeSiteNewsClassDao) {
		this.homeSiteNewsClassDao = homeSiteNewsClassDao;
	}





	/******
	 * 批量上传新闻
	 */
	public List<BatchUploadHomeSiteNewsBean> getAllUploadNews(File file) {
		
		try {
			Workbook wb = Workbook.getWorkbook(file);
			
			Sheet sheet[] = wb.getSheets();
			
			for (int i = 0; i < sheet.length; i++) {
				Sheet stTemp = sheet[i];
               
				// 得到总列数
				int intColumns = stTemp.getColumns();
				// 得到总行数
				int intRows = stTemp.getRows();

				//头上的对象
				Map<String,HeadBean> headBeanMap=new HashMap<String,HeadBean>();
				//正式的内容对象
				List<BatchUploadHomeSiteNewsBean> batchUploadHomeSiteNewsBeanList=new ArrayList<BatchUploadHomeSiteNewsBean>();
				for (int j = 0; j < intRows; j++) {
					
//					获取头部的bean
			    	Cell[] cells = stTemp.getRow(j);
			    	
					    //如果是内容
					    if(j!=0){
					    	
								BatchUploadHomeSiteNewsBean batchUploadHomeSiteNewsBean = this.getContext(cells,headBeanMap);
								batchUploadHomeSiteNewsBeanList.add(batchUploadHomeSiteNewsBean);
							
		                }
					    //如果是头部
					    else{
		                	
					    	headBeanMap = this.getHeadBean(cells);
		                }
					
				}
				
				
			  return batchUploadHomeSiteNewsBeanList;
			}
			
		} catch (Exception e) {
			
			logger.error("批量上传新闻失败",e);
		} 
		
		return null;
		
	}

	
	
	
	
	/**********
	 * 获取当前行的内容 
	 * @param cells
	 * @return
	 */
	private BatchUploadHomeSiteNewsBean getContext(Cell[] cells,Map<String,HeadBean> headBeanMap) {
		
		BatchUploadHomeSiteNewsBean batchUploadHomeSiteNewsBean = new BatchUploadHomeSiteNewsBean();
		if(cells!=null){
			
			for(int j=0;j<cells.length;j++){
				//头
				HeadBean curRowHead = headBeanMap.get(j+"");
				Cell cell = cells[j];
				String content = "";
				if(cell!=null){
					content =  cell.getContents();
				}
				if(curRowHead!=null  && curRowHead.getCurName().equals("栏目名称")){
					//通过名称获取类目id
					HomeSiteNewsClass   homeSiteNewsClass =homeSiteNewsClassDao.loadNewsClassByNames(content);
					if(homeSiteNewsClass!=null){
						batchUploadHomeSiteNewsBean.setClassName(homeSiteNewsClass.getNames());
						batchUploadHomeSiteNewsBean.setHomeSiteNewsClass(homeSiteNewsClass);
					}
					
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("发表人用户名")){
					//通过用户名获取id
					UserBase  userBase =userBaseDao.getUserBaseByUserName(content);
					if(userBase!=null){
						batchUploadHomeSiteNewsBean.setUserBase(userBase);
					}
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("标题")){
					batchUploadHomeSiteNewsBean.setTitle(content);
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("内容")){
					batchUploadHomeSiteNewsBean.setContent(content);
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("关键字")){
					batchUploadHomeSiteNewsBean.setKeyword(content);
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("作者")){
					batchUploadHomeSiteNewsBean.setWriter(content);
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("来源")){
					batchUploadHomeSiteNewsBean.setSource(content);
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("是否推荐")){
					int commend=0;
					if(content!=null&& !content.equals("")){
						commend = Integer.valueOf(content);
					}
					batchUploadHomeSiteNewsBean.setIscommend(commend);
				}
				
				else if(curRowHead!=null  && curRowHead.getCurName().equals("是否图片新闻")){
					short isImg=0;
					if(content!=null&& !content.equals("")){
						isImg = Short.valueOf(content);
					}
					batchUploadHomeSiteNewsBean.setIsImg(isImg);
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("图片")){
					
					//调用上传图片的逻辑
					
					batchUploadHomeSiteNewsBean.setImg(content);
				}
				else if(curRowHead!=null  && curRowHead.getCurName().equals("是否置顶")){
					short isTop=0;
					if(content!=null&& !content.equals("")){
						isTop = Short.valueOf(content);
					}
					batchUploadHomeSiteNewsBean.setToptitle(isTop);
				}
				
				else if(curRowHead!=null  && curRowHead.getCurName().equals("是否审核")){
					short audite=0;
					if(content!=null&& !content.equals("")){
						audite = Short.valueOf(content);
					}
					batchUploadHomeSiteNewsBean.setAudite(audite);
				}
				
				
				
			}
			
		}
		
		return batchUploadHomeSiteNewsBean;
		
		 
	}

	
	/********
	 * 获取头的信息
	 * @param cells
	 * @return
	 */
	private Map<String,HeadBean>  getHeadBean(Cell[] cells) {
		
		Map<String,HeadBean> hashMap = new HashMap<String,HeadBean>();
		if(cells!=null){
			for(int j=0;j<cells.length;j++){
				Cell cell = cells[j];
				String content = cell.getContents();
				if(content!=null   && !content.equals("")){
					HeadBean headBean = new HeadBean();
					headBean.setCurRow(j);
					headBean.setCurName(content);
					headBean.setType("0");
					hashMap.put(""+j, headBean);
				}
				
			}
		}
		
		return hashMap;
	}





	public boolean uploadNews(File file) {
		
		List<BatchUploadHomeSiteNewsBean> batchUploadHomeSiteNewsBeanList = getAllUploadNews(file);
		if(batchUploadHomeSiteNewsBeanList!=null  && !batchUploadHomeSiteNewsBeanList.isEmpty()){
			//将这些数据,添加到数据库中
			for(BatchUploadHomeSiteNewsBean b:batchUploadHomeSiteNewsBeanList){
				this.addNewsToDB(b);
			}
		}
		
		return true;
		
	}




    /*******
     * 添加到数据库中
     * @param b
     */
	private void addNewsToDB(BatchUploadHomeSiteNewsBean b) {
		
		 try {
			HomeSiteNews homeSiteNews=new HomeSiteNews();
			 if(b!=null){
				 BeanUtils.copyProperties(homeSiteNews, b);
			 }
			 
			 //通过类目名称,获取类目对象
			 if(b.getHomeSiteNewsClass()==null){
				 HomeSiteNewsClass homeSiteNewsClass = this.homeSiteNewsClassDao.loadNewsClassByNames(b.getClassName(), 1);
				 
				 if(homeSiteNewsClass!=null){
					 homeSiteNews.setHomeSiteNewsClass(homeSiteNewsClass);
				 }
			 }
			
			 if(homeSiteNews.getHomeSiteNewsClass()!=null){
				 homeSiteNews.setCreateDatetime(new Date());
				 homeSiteNews.setUpdateDatetime(new Date());
				 homeSiteNews.setIsDel(new Short("0"));
				 this.homeSiteNewsDao.add(homeSiteNews);
			 }
			
			
			 
			 
		} catch (Exception e) {
			this.logger.error("addNewsToDB error",e);
		} 
	}



}
